package club.guadazi.wemarket.controller.web;

import club.guadazi.wemarket.entity.OrderSimpleInfo;
import club.guadazi.wemarket.service.OrderSimpleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/order")
public class OrderSimpleController {
    private final static Logger log = LoggerFactory.getLogger(OrderSimpleController.class);
    @Autowired
    private OrderSimpleService orderSimpleService;

    @RequestMapping()
    public String index() {
        return "order/index";
    }

    @ResponseBody
    @RequestMapping(value = "search.do", method = RequestMethod.POST)
    public Map<String, Object> searchLikeName(@RequestParam(value = "keyWords", required = true) String name, Model model) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<OrderSimpleInfo> orderSimpleInfoList = null;
        if (!StringUtils.isEmpty(name)) {
            try {
                orderSimpleInfoList = orderSimpleService.searchLikeName(name);
                resultMap.put("msg", "true");
            } catch (Exception e) {
                resultMap.put("msg", e.getMessage());
                log.error(e.getMessage());
                e.printStackTrace();
            }
        }
        resultMap.put("data", orderSimpleInfoList);
        model.addAttribute("result", resultMap);
        return resultMap;
    }
}
